package com.sysassist.servicemgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.sysassist.R;

import android.app.ActivityManager;
import android.app.ListActivity;
import android.app.ActivityManager.RunningServiceInfo;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class ServiceMgrActivity extends ListActivity {
	/** RunningServiceInfo */
	private static List<RunningServiceInfo> serviceList = null;
	/** maxServiceNum */
    private static final int maxServiceNum = 100;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serv_list);
        
        serviceList = new ArrayList<RunningServiceInfo>();
        getServiceInfo();
        showServiceInfo();
    }
    
    /**
     * showServiceInfo
     */
    private void showServiceInfo() {
        List<HashMap<String,String>> infoList = new ArrayList<HashMap<String,String>>();
        for (Iterator<RunningServiceInfo> iterator = serviceList.iterator(); iterator.hasNext();) {
            RunningServiceInfo serviceInfo = iterator.next();
            HashMap<String, String> map = new HashMap<String, String>();
            String serviceNameStr = serviceInfo.service.toString();
            int startIndex = serviceNameStr.indexOf("{");
            int endIndex = serviceNameStr.indexOf("}");
            String serviceName = serviceNameStr.substring(startIndex + 1, endIndex);
            map.put("service_name", "所属类名:" + serviceName);
            map.put("service_proc_name", "所属进程:" + serviceInfo.process);
            map.put("service_pid", "PID:" + serviceInfo.pid);
            infoList.add(map);
        }
        
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this, 
                infoList, 
                R.layout.serv_list_item, 
                new String[] { "service_name", "service_proc_name", "service_pid" },
				new int[] { R.id.service_name, R.id.service_proc_name, R.id.service_pid });
        setListAdapter(simpleAdapter);
    }
    
    /**
     * getServiceInfo
     * @return
     */
    private int getServiceInfo() {
        ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        serviceList = activityManager.getRunningServices(maxServiceNum);
        return serviceList.size();
    }
    
    /**
     * ActivityManager.RunningServiceInfo {
     *     public long activeSince        // 服务第一次被激活的时间 (启动和绑定方式)
     *     public int clientCount        // 连接到该服务的客户端数目
     *     public int clientLabel        // 【系统服务】为客户端程序提供用于访问标签
     *     public String clientPackage    // 【系统服务】绑定到该服务的包名
     *     public int crashCount        // 服务运行期间，出现crash的次数
     *     public int flags                // 服务运行的状态标志
     *     public boolean foreground    // 服务是否被做为前台进程执行
     *     public long lastActivityTime    // 该服务的最后一个活动的时间
     *     public int pid                // 非0值，表示服务所在的进程Id
     *     public String process        // 服务所在的进程名称
     *     public long restarting        // 如果非0，表示服务没有执行，将在参数给定的时间点重启服务
     *     public ComponentName service    // 服务组件信息
     *     public boolean started        // 标识服务是否被显示的启动
     *     public int uid                // 拥有该服务的用户Id
     * }
     */
}
